<template>
    <view class="pub_page pub_set pub_header_padding">
        <pub-head headerName="mine_set" :headerConfig="headerConfig"></pub-head>
        <pub-dialog v-show="dialogShow" dialogName="mine_set" :dialogConfig="dialogConfig"
            @dialogBack="dialogBack"></pub-dialog>
        <div class="set_box pub_page_scroll">
            <div class="lis">
                <div class="li li_1" @click="$comFun.openWin('/pages/mine/info')">个人信息
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                </div>
                <div class="li li_1" @click="$comFun.openWin('/pages/other/address',{openType:1})">收货地址
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                </div>
                <!-- <div class="li li_5" @click="zxDialogShow = true;">注销账户
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                </div> -->
            </div>
            <div class="lis" v-if="userInfo">
                <div class="li li_2" @click="$comFun.openWin('/pages/login/find_pay_psd', { mobile: mobile })">修改二级密码
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                </div>
                <div class="li li_2" @click="$comFun.openWin('/pages/login/find_psd', { mobile: mobile, openType: 2 })">
                    修改登录密码
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                </div>
            </div>
            <div class="lis">
                <div class="li li_3" v-if="userInfo && userInfo.province">所属区域
                    <span class="txt">{{ userInfo.province }}{{ userInfo.city }}{{ userInfo.region }}</span>
                </div>
                <div class="li li_3" v-else @click="getUserLocation">所属区域
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                    <span class="txt">点击获取所属区域</span>
                </div>
            </div>
            <div class="lis">
                <div class="li li_4" @click="$comFun.openWin('/pages/other/about_us')">关于我们
                    <text class="icon-font-basic icon-b-arrow-1-2"></text>
                </div>
            </div>
            <!-- <div class="lis">
                <div class="li" @click="clearInfo">清除缓存</div>
                <div class="li">版本号 <span class="txt">{{ version }}</span></div>
            </div> -->
            <br>
            <br>
            <div class="pub_btn lis_btn" @click="toLogout">退出登录</div>
            <br>
            <br>
        </div>


        <view class="pub_dialog" v-if="zxDialogShow">
            <div class="d_box">
                <div class="d_title">注销</div>
                <div class="d_content">
                    <div class="input_div_new">
                        <div class="li li_1">
                            <input class="input" v-model="mobile" type="number" disabled>
                        </div>
                        <div class="li li_2">
                            <input class="input" v-model="code" type="number" placeholder="请输入验证码" maxlength="6">
                            <div class="input_right_btn" @click="toSendCode">{{ code_msg }}</div>
                        </div>
                        <div class="li_txt">注：注销后账号就不可使用，此操作不可逆</div>
                    </div>
                </div>
                <div class="d_btns">
                    <div class="d_btn" @click="zxDialogShow = false;">取消</div>
                    <div class="d_btn" @click="sureZhuxiao">确定</div>
                </div>
            </div>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            pagescrollTop: 0,
            headerConfig: {
                title: '设置',//标题栏内容
                left_type: 3,
            },
            mobile: '',

            dialogShow: false,
            dialogType: '',
            dialogConfig: {
                content: ''
            },

            zxDialogShow: false,
            code: '',
            code_msg: '发送验证码',

            userInfo: '',
            locationInfo: '',

            version: '',
            isRecommend: true,
        };
    },
    onLoad(option) {
        this.getInfo();
        // #ifdef APP-PLUS
        // that.version = plus.runtime.version;
        // plus.runtime.getProperty(plus.runtime.appid, function (wgtinfo) {
        //     that.version = wgtinfo.version;
        // });
        // #endif
    },
    onPageScroll(res) {
        this.pagescrollTop = res.scrollTop;
    },
    methods: {
        async getInfo() {
            this.$comFun.openLoading();
            const res = await this.$request.get('user/userInfo', {});
            this.$comFun.closeLoading();
            // console.log(res);
            if (res.code == 200) {
                this.userInfo = res.result;
                this.mobile = res.result.mobile;
            }
        },
        async getUserLocation() {
            // #ifdef APP-PLUS
            const that = this;
            that.$comFun.openLoading();
            // result 1=>已获取授权  0=>未获取授权-1=>被永久拒绝授权
            let result = 0;
            if (this.$store.state.deviceType == 'ios') {
                result = await this.$permision.judgeIosPermission('location');
            } else {
                result = await this.$permision.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
            }
            // console.log("result:", result);
            if (result == 1 || result == true) {
                uni.getLocation({
                    type: 'gcj02',
                    geocode: true,
                    success: function (res) {
                        // console.log(res);
                        that.locationInfo = {
                            latitude: res.latitude,
                            longitude: res.longitude,
                            // address_txt: `${res.address.province} ${res.address.city} ${res.address.district}`,
                            // address: res.address,
                        };
                        that.saveUserLocation();
                    },
                    fail: function (err) {
                        // console.log(err);
                        that.$comFun.closeLoading();
                        that.$comFun.toast('获取定位信息失败，请稍后再试');
                    }
                });
            } else {
                that.$comFun.closeLoading();
                that.$comFun.toast('请先打开定位权限');
            }
            // #endif
        },
        async saveUserLocation() {
            uni.setStorageSync('userLocationInfo', this.locationInfo);
            const res = await this.$request.post('user/bindPosition', {
                lat: this.locationInfo.latitude,
                lon: this.locationInfo.longitude,
            });
            if (res.code == 200) {
                this.$comFun.toast('定位成功');
                this.getInfo();
            } else {
                this.$comFun.closeLoading();
                this.$comFun.toast(res.message);
            }

            // console.log(res);
        },
        clearInfo() {
            this.$comFun.clearAllInfo();
            this.$comFun.toast('清除成功');
        },
        toLogout() {
            this.dialogShow = true;
            this.dialogType = 'sureLogout';
            this.dialogConfig.content = '是否确定进行退出当前登录？';
        },
        // toZhuxiao() {
        //     this.zxDialogShow = true;
        // },
        async sureZhuxiao() {
            if (this.code.length != 6) {
                this.$comFun.toast('请输入正确的验证码');
                return;
            }
            this.$comFun.openLoading();
            const res = await this.$request.get('user/logout', {
                code: this.code
            });
            // console.log(res);
            this.$comFun.closeLoading();
            if (res.code == 200) {
                setTimeout(() => {
                    this.$comFun.toast('注销成功');
                    this.$comFun.clearUserInfo();
                }, 100)
            } else {
                this.$comFun.toast(res.message);
            }
        },
        async toSendCode() {
            if (this.mobile.length != 11) {
                // this.$comFun.toast('请输入正确的手机号');
                return;
            }
            if (this.code_msg == '发送验证码') {
                this.$comFun.openLoading();
                const res = await this.$request.get('public/sendSms', {
                    mobile: this.mobile
                });
                // console.log(res);
                this.$comFun.closeLoading();
                if (res.code == 200) {
                    this.codeTime();
                } else {
                    this.$comFun.toast(res.message);
                }
            }
        },
        codeTime() {
            this.code_msg = '剩 60 s';
            let count = 60;
            let time = setInterval(() => {
                if (count == 1) {
                    clearInterval(time);
                    if (this.$comFun.ValidateMobile(this.mobile)) {
                        this.code_msg = '发送验证码';
                    } else {
                        this.code_msg = '请输入手机号';
                    }
                    return;
                }
                count--;
                this.code_msg = '剩 ' + count + ' s';
            }, 1000);
        },
        async dialogBack(bdata) {
            this.dialogShow = false;
            if (bdata.name == 'mine_set' && bdata.key == 1) {
                if (this.dialogType == 'sureLogout') {
                    this.$comFun.toast('退出成功');
                    this.$comFun.clearUserInfo();
                    this.$txChat.logout().then(function (imResponse) {
                        console.log("chat-logout",imResponse); // 登出成功
                    }).catch(function (imError) {
                        console.warn('logout error:', imError);
                    });

                }
            }
        }
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/other.scss";
</style>
